import doInterce.impl.DoBatSql;
import enums.DatabaseTypeEnum;
import org.yaml.snakeyaml.Yaml;
import utils.DBUtil;
import utils.ExceptionUtil;
import utils.YmlUtil;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

/**
 * @Description:
 * @Author: syh
 * @Date: 2024/01/04/16:29
 */
public class Main {
    /**
     * 第一个参数为数据库类型，第二个参数为自定义参数
     * @param args
     * @throws SQLException
     */
    public static void main(String[] args) throws SQLException {
        String databaseType = DatabaseTypeEnum.MYSQL.getDatabaseName();
        String customParam = "";
        if(args != null && args.length > 0){
            databaseType = args[0];
        }
        if(args != null && args.length > 1){
            customParam = args[1];
        }
        System.out.println(customParam);
        /**
         * 读取配置文件
         */
        Yaml yaml = new Yaml();
        InputStream resourceAsStream = Main.class.getResourceAsStream("config.yml");
        Map<String, Object> data = yaml.load(resourceAsStream);
        YmlUtil.setYmlMap(data);


        Connection connection = DBUtil.getConnection(databaseType);
        try {
            DoBatSql doBatSql = new DoBatSql();
            doBatSql.doRun(connection,customParam);
            connection.close();
            System.out.println("执行成功");
        }catch (Exception e){
            System.out.println("系统出现异常");
            ExceptionUtil.writeLog(e);
        }

        connection.close();

    }
}
